package me.victor.jetpackcompose

import org.junit.Assert.assertEquals
import org.junit.Test
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy


/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun listBreak() {
        val list = listOf(1, 2, 3, 4, 5)
//        // 不需要返回符合条件的结果
        list.any {
//            val bool = it == 6
            val bool = it == 3
            println("it = $it, bool = $bool")
            bool
        }
        // 需要返回符合条件的结果
        val result = list.firstOrNull {
//            val bool = it == 6
            val bool = it == 3
            println("it = $it, bool = $bool")
            bool
        }
        println("result = $result")
    }

    @Test
    fun finally() {
        try {
            println("try")
            val service = Proxy.newProxyInstance(
                Service::class.java.classLoader,
                arrayOf(Service::class.java),
                object : InvocationHandler {
                    override fun invoke(proxy: Any, method: Method, args: Array<Any>?): Any? {
                        if (method.declaringClass === Any::class.java) {
                            if (args.isNullOrEmpty()) {
                                return method.invoke(this)
                            }
                            return method.invoke(this, args)
                        }
                        if (method.name == "getUsername") {
                            return "Victor Zheng"
                        }
                        return null
                    }
                }) as Service
            println(service.javaClass.name)
            println(service.toString())
            println(service.getUsername())
//            throw RuntimeException()
//            exitProcess(0)
            return
        } catch (e: Exception) {
            println("catch")
            e.printStackTrace()
//            return
        } finally {
            println("finally")
        }
    }
}